package com.k.zero.qrscan.qrscan;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;

public class CustomScanActivity extends Activity{

    private DecoratedBarcodeView mDBV;
    private CaptureManager captureManager;

    private static LinearLayout layout_light_tip;
    private static ImageView img_light ;
    private static Boolean isLight = false;

    private ImageView imageView;

    @Override
    protected void onPause() {
        super.onPause();
        captureManager.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        captureManager.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        captureManager.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        captureManager.onSaveInstanceState(outState);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        return super.onKeyDown(keyCode, event);
//        return mDBV.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_scan);

        mDBV = (DecoratedBarcodeView) findViewById(R.id.dbv_custom);
        mDBV.getStatusView().setText("将二维码放入框内，即可扫描");

        //重要代码，初始化捕获
        captureManager = new CaptureManager(this,mDBV);
        captureManager.initializeFromIntent(getIntent(),savedInstanceState);
        captureManager.decode();

        img_light = (ImageView) findViewById(R.id.img_light);
        layout_light_tip = (LinearLayout) findViewById(R.id.layout_light_tip);
        layout_light_tip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isLight){
                    isLight = false;
                    mDBV.setTorchOff();
                    img_light.setImageResource(R.mipmap.ico_qrcode_light_off);
                }else {
                    isLight = true;
                    mDBV.setTorchOn();
                    img_light.setImageResource(R.mipmap.ico_qrcode_light_on);
                }
            }
        });

        //扫描线动画
        imageView = (ImageView) findViewById(R.id.scanner_line);
        imageView.setVisibility(View.VISIBLE);
        if (imageView.getAnimation() == null) {
            TranslateAnimation anim = new TranslateAnimation(0, 0, 0,dip2px(CustomScanActivity.this, 250));
            anim.setDuration(2500);
            anim.setRepeatCount(Animation.INFINITE);
            imageView.startAnimation(anim);
        }


    }

    /**
     * 获取图片亮度
     * @param bm
     * @return
     */
    public static int getBright(Bitmap bm) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        int r, g, b;
        int count = 0;
        int bright = 0;
        for(int i = 0; i < width; i++) {
            for(int j = 0; j < height; j++) {
                count++;
                int localTemp = bm.getPixel(i, j);
                r = (localTemp | 0xff00ffff) >> 16 & 0x00ff;
                g = (localTemp | 0xffff00ff) >> 8 & 0x0000ff;
                b = (localTemp | 0xffffff00) & 0x0000ff;
                bright = (int) (bright + 0.299 * r + 0.587 * g + 0.114 * b);
            }
        }
        return bright / count;
    }

    /**
     * 将亮度值传送至 mHandler
     * @param light
     */
    public static void showLight(final int light){
        mHandler.sendEmptyMessage(light);
    }

    /**
     *
     */
    private static Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg){
//            Log.e("Message",msg.what+"  "+p.getFlashMode());
            if (msg.what <= 60){
                layout_light_tip.setVisibility(View.VISIBLE);
            }else {
                if (isLight){
                    layout_light_tip.setVisibility(View.VISIBLE);
                    img_light.setImageResource(R.mipmap.ico_qrcode_light_on);
                }else {
                    layout_light_tip.setVisibility(View.GONE);
                }
            }
            super.handleMessage(msg);
        }
    };

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
